clear all
clear matrix
version 13
capture log close
set more off, permanently

*-------------------------------------------------------------------------------
* Options	
*-------------------------------------------------------------------------------
	global main		= "\\iab.baintern.de\DFS\017\Ablagen\D01700-Data\N2066017\Datenaustausch\GradAB\Schnitzlein_Stueber\Stueber\CADAL_Flow_Dataset\Federica"
	global data_orig= "N:\Ablagen\D01700-IAB-Projekte\D01700-CADAL_Flow_Dataset\package"
	global data_proc= "$main"+"\"+"temp"
	global log   	= "$main"+"\"+"log"
	global do		= "$main"+"\"+"do"
	global data_add = "$main"+"\"+"data"
*-------------------------------------------------------------------------------
cd "$main"
*-------------------------------------------------------------------------------
********************************************************************************
* INPUT: RAW DATA
* OUTPUT: GET QUARTERLY LOCAL/INDUSTRY-SPECIFIC S.A. EMPLOYMENT TIME SERIES
********************************************************************************
use "$data_proc\data_federica_1990_2014_west.dta", clear 

gen quarter = q - 4 * (a - 1)
label var a "index of year"
label var year "year"
label var st_reg "stock regular workers"
label var founded_q "Quarter in which establishment founded"
label var quarter "Quarter (1-4)"
label var district "district (Kreis) of establishment"

rename st_reg employment

* Count of (new) establishments:
gen count_all = 1
gen count_new = 0
replace count_new = 1 if q == founded_q

* Create time variable:
gen eventdate=yq(year,quarter)
format eventdate %tq
drop year quarter

rename w93_imp w93_3
sum employment 
count if employment==.
count if employment==.z
********************************************************************************
* Drop public sector & education 
drop if w93_3>=750 & w93_3<900
********************************************************************************
* Drop agriculture and mining:
drop if w93_3<150
********************************************************************************
* Get info on MAs:
qui: ren district krs
qui: destring krs, force replace

merge m:1 krs using "$data_add/amr_krs_72.dta"
drop if _merge!=3
drop _merge
********************************************************************************
egen id_ind = group(id w93_2)
by id_ind, sort: egen totemployment = total(employment)
keep totemployment eventdate amr w93_2
duplicates drop
egen id_new = group(amr w93_2)

preserve
keep id_new amr w93_2
duplicates drop
saveold "$data_proc/id_new.dta", replace
restore

drop amr w93_2
reshape wide totemployment, i(eventdate) j(id_new)

tsset eventdate
sum id_new
local max = `r(max)'

forv n = 1(1)`max'{
di `n'
cap seasonallyadjust totemployment`n', gen(totemployment_sa`n') ar(1) ma(1) x11 multiplicative replace
if _rc!=0{
gen totemployment_sa`n'=.
}
else{
}
}

keep totemployment_sa* eventdate 
reshape long totemployment_sa, i(eventdate) j(id_new)
merge m:1 id_new using "$data_proc/id_new.dta"
drop _merge
saveold "$data_proc/industryemployment_sa.dta", replace
********************************************************************************
